package com.ghh.dao;

import com.ghh.pojo.Order;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;

import java.util.List;
import java.util.Map;

public interface OrderDao {

    //分页查询
    @Select("<script>select t_order.orderDate,t_member.fileNumber,t_member.`name`,t_member.phoneNumber,t_order.orderType,t_order.orderStatus,t_setmeal.`name` sname " +
                "from t_order inner join t_member on t_order.member_id=t_member.id " +
                    "inner join t_setmeal on t_order.setmeal_id = t_setmeal.id <if test=\"value!=null and value!=''\"> " +
                        "where t_member.fileNumber like '%$#{value}%' or t_member.`name` like '%$#{value}%' or t_member.phoneNumber like '%$#{value}%'</if></script>")
    Page<Map<String,Object>> findByCondition(@Param("value") String queryString);

    //根据会员id，日期和套餐id查询预约信息的条数
    @Select("select count(*) from t_order where member_id=#{memberId} and orderDate=#{orderDate} and setmeal_id=#{setmealId}")
    Integer findCountByCondition(Order order);

    //添加预约信息并返回预约的id
    @Insert("insert into t_order(id,member_id,orderDate,orderType,orderStatus,setmeal_id,orderCompany_id) values(#{id},#{memberId},#{orderDate},#{orderType},#{orderStatus},#{setmealId},#{orderCompany_id})")
    @SelectKey(statement = "select last_insert_id()", before = false, keyProperty = "id", resultType = Integer.class, keyColumn = "id")
    void add(Order order);


    //根据id查询预约信息、体检人信息和套餐信息
    @Select("select t_member.name member ,t_setmeal.name setmeal,t_order.orderDate orderDate,t_order.orderType orderType ,t_company.address " +
            "from t_order,t_member,t_setmeal,t_company " +
            "where t_order.member_id=t_member.id and t_order.setmeal_id=t_setmeal.id " +
            "and t_order.orderCompany_id=t_company.id and t_order.id=#{id}")
    Map findById(Integer id);

    @Select("select count(id) from t_order where orderDate=#{reportDate}")
    Integer findOrderCountByDate(String reportDate);

    @Select("select count(id) from t_order where orderDate>#{date}")
    Integer findOrderCountAfterDate(String date);

    @Select("select count(id) from t_order where orderDate=#{reportDate} and orderStatus='已到诊'")
    Integer findVisitsCountByDate(String reportDate);

    @Select("select count(id) from t_order where orderDate>#{date} and orderStatus='已到诊'")
    Integer findVisitsCountAfterDate(String date);

    @Select("select (select name from t_setmeal where id= setmeal_id) name, count(id) setmeal_count, count(id)/(select count(id) from t_order) proportion from t_order group by setmeal_id order by setmeal_count desc limit 4")
    List<Map<String, Object>> findHotSetmeal();
}
